﻿///<copyright>
/// Copyright © 2009 Stephen Styrchak. All Rights Reserved.
/// Portions copyright (C) Microsoft Corporation. All rights reserved.
///</copyright>
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ferpect.ComponentModel.Sprites
{
    public class SmokePlumeParticlesSprite : ParticlesSprite
    {
        public SmokePlumeParticlesSprite()
        {
            MinInitialSpeed = 20;
            MaxInitialSpeed = 100;

            // we don't want the particles to accelerate at all, aside from what we
            // do in our overriden InitializeParticle.
            MinAcceleration = 0;
            MaxAcceleration = 0;

            // long lifetime, this can be changed to create thinner or thicker smoke.
            // tweak minNumParticles and maxNumParticles to complement the effect.
            MinLifetime = 5.0f;
            MaxLifetime = 7.0f;

            MinScale = 0.5f;
            MaxScale = 1.0f;

            MinNumParticles = 7;
            MaxNumParticles = 15;

            // rotate slowly, we want a fairly relaxed effect
            MinRotationSpeed = -MathHelper.PiOver4 / 2.0f;
            MaxRotationSpeed = MathHelper.PiOver4 / 2.0f;

            // additive blending is very good at creating fiery effects.
            BlendState = BlendState.AlphaBlend;
        }

        /// <summary>
        /// PickRandomDirection is overriden so that we can make the particles always 
        /// have an initial velocity pointing up.
        /// </summary>
        /// <returns>a random direction which points basically up.</returns>
        protected override Vector2 PickRandomDirection()
        {
            // Point the particles somewhere between 80 and 100 degrees.
            // tweak this to make the smoke have more or less spread.
            float min = MathHelper.ToRadians(80);
            float max = MathHelper.ToRadians(100);
            float radians = min + (float)Particle.Random.NextDouble() * (max - min);

            Vector2 direction = Vector2.Zero;
            // from the unit circle, cosine is the x coordinate and sine is the
            // y coordinate. We're negating y because on the screen increasing y moves
            // down the monitor.
            direction.X = (float)Math.Cos(radians);
            direction.Y = -(float)Math.Sin(radians);
            return direction;
        }

        protected override void InitializeParticle(ref Particle particle, Microsoft.Xna.Framework.Vector2 where)
        {
            base.InitializeParticle(ref particle, where);
            // the base is mostly good, but we want to simulate a little bit of wind
            // heading to the right.
            particle.Acceleration.X += (10f + (float)Particle.Random.NextDouble() * 40f) * this.Scale;
        }
    }
}
